use crate::cblash_h::IndexOffset;



pub fn cblas_saxpy (N: usize, alpha: f64, X: &Vec<f64>, incX: i32, Y: &mut Vec<f64>, incY: i32,) {
// #include "source_axpy_r.h"
//   INDEX i;
    if N < 1 {
        panic!("cblas_saxpy: N < 1");
    }
    if alpha == 0.0 {
        return;
    }

    if incX == 1 && incY == 1 {
        let m = N % 4;
        if X.len() < N || Y.len() < N {
            panic!("cblas_saxpy: X.len() < N || Y.len() < N (incX == 1 && incY == 1)");
        } 

        for index in 0..m {
            Y[index] += alpha * X[index];
        }
        let mut index = m;
        loop {
            if index + 3 >= N {
                break;
            }
            Y[index] += alpha * X[index];
            Y[index + 1] += alpha * X[index + 1];
            Y[index + 2] += alpha * X[index + 2];
            Y[index + 3] += alpha * X[index + 3];

            index += 4;
        }
    } else {
        let ix = IndexOffset::new(N, incX);
        let iy = IndexOffset::new(N, incY);
        for i in 0..ix.indices.len() {
            Y[iy.indices[i]] += alpha * X[ix.indices[i]];
        }
    }
  
}